<template>
  <s-table
    ref="table"
    size="default"
    :columns="columns"
    :data="loadData"
    rowKey="recordId"
    :alert="false"
    bordered
    :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
  >
    <span slot="action" slot-scope="text, record">
      <template v-if="$auth('table.update')">
        <a @click="$emit('edit', record)">编辑</a>
        <a-divider type="vertical" />
        <a @click="$emit('delete', record)">删除</a>
      </template>
    </span>
  </s-table>
</template>

<script>
// import { STable } from '@/components'
import { getServiceList } from '@/api/manage'

export default {
  name: 'RecordTable',
  components: {
    STable
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: 'No',
          dataIndex: 'no'
        },
        {
          title: '编号',
          dataIndex: 'description'
        },
        {
          title: '用户名',
          dataIndex: 'callNo',
          sorter: true,
          needTotal: true,
          customRender: text => text + ' 次'
        },
        {
          title: '事件',
          dataIndex: 'status',
          needTotal: true
        },
        {
          title: '最近登录',
          dataIndex: 'updatedAt',
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getServiceList(Object.assign(parameter, this.queryParam)).then(res => {
          return res.result
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleQuery(queryParam) {
      this.queryParam = queryParam
      this.$refs.table.refresh(true)
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.$emit('selectChange', selectedRowKeys, selectedRows)
    }
  }
}
</script>
